In this code, we'll define a function to generate a random portfolio weights, perform an asset allocation, and then and analyze their returns.
Install Libs. (remove comments '#' if need to install the libraries)
# !pip install pandas
# !pip install pandas-datareader
# !pip install numpy
# !pip install plotly_express
# !pip install random
#import Libraries
import pandas as pd
from pandas_datareader import data as pdr
import numpy as np
import random
import plotly.graph_objects as go
functions already defined that we use on this code
# Define a function using Plotly Express
def plotly_data(df, title):
# Create figure
fig = go.Figure()
# Set title
fig.update_layout(title_text = title)
# For loop that plots all stock prices in the pandas dataframe df
for i in df.columns[0:]:
# Add range slider
#fig.update_layout(xaxis=dict(rangeselector = dict(buttons=list([dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all")])), rangeslider=dict( visible=True), type="date"))
# Add line graph
fig.add_scatter(x = df.index, y = df[i], name = i)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def log_plotly_data(df, title):
# Create figure
fig = go.Figure()
# Set title
fig.update_layout(title_text = title)
# For loop that plots all stock prices in the pandas dataframe df
for i in df.columns[0:]:
# Add range slider
#fig.update_layout(xaxis=dict(rangeselector = dict(buttons=list([dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all")])), rangeslider=dict( visible=True), type="date"))
# Add line graph
fig.add_scatter(x = df.index, y = df[i], name = i)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.update_yaxes(type="log")
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def plotly_line(df, y, title):
# Create figure
fig = go.Figure()
fig.update_layout(title_text = title)
fig.add_scatter(x = df.index, y = y)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def log_plotly_line(df, y, title):
# Create figure
fig = go.Figure()
fig.update_layout(title_text = title)
fig.add_scatter(x = df.index, y = y)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.update_yaxes(type="log")
fig.show()
# Function to scale stock prices based on their initial starting price
# The objective of this function is to set all prices to start at a value of 1
def price_scaling(raw_prices_df):
scaled_prices_df = raw_prices_df.copy()
for i in raw_prices_df.columns[0:]:
scaled_prices_df[i] = raw_prices_df[i]/raw_prices_df[i][0]
return scaled_prices_df
file_name = input('Input the CSV file name: ')
initial_investment = int(input('Input the initial investment: '))
n_runs = int(input('Input the number of simulations: '))
## BRL5
# PETR4.SA VALE3.SA TAEE11.SA ITSA4.SA WEGE3.SA
## MAG7
# AAPL AMZN GOOG META MSFT NVDA TSLA
## BRL_top10
# CRFB3.SA CPLE6.SA ECOR3.SA ITUB4.SA JBSS3.SA RENT3.SA PRIO3.SA SBSP3.SA VALE3.SA VIVT3.SA
Input the CSV file name: BRL5 Input the initial investment: 50000 Input the number of simulations: 10
#read CSV file
Stock_Prices_df = pd.read_csv(file_name)
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
Stock_Prices_df.set_index(['Date'], inplace = True)
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
#lock vector to test function
weights = np.ones(n_assets) * 1/n_assets
weights
array([0.2, 0.2, 0.2, 0.2, 0.2])
#Define a function that performs an Asset Allocation
def asset_allocation(df, initial_investment, weights):
''' Performs an asset Allocation for a given DF, initial investment value and weights'''
portfolio_df = df.copy()
# Scale stock prices using the "price_scaling"
scaled_df = price_scaling(df)
#enumerate method links Stocks tickers in columns along with a counter position weight (i), like an index
for i, stock in enumerate(scaled_df):
portfolio_df[stock] = weights[i] * scaled_df[stock] * initial_investment
# Sum up all values and place the result in a new column titled "portfolio value [$]"
portfolio_df['Total Value [$]'] = portfolio_df.sum(axis = 1, numeric_only = True)
# Calculate the portfolio percentage daily return and replace NaNs with zeros
portfolio_df['Daily Return [%]'] = portfolio_df['Total Value [$]'].pct_change(1) * 100
portfolio_df.replace(np.nan, 0, inplace = True)
return portfolio_df
#Asset Allocation with parameters defined
portfolio_df = asset_allocation(Stock_Prices_df, initial_investment, weights)
Eqw = portfolio_df
portfolio_df.round(2)
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 10000.00 | 10000.00 | 10000.00 | 10000.00 | 10000.00 | 50000.00 | 0.00 |
| 2013-12-17 | 10000.00 | 9688.58 | 9894.73 | 9965.11 | 9951.94 | 49500.36 | -1.00 |
| 2013-12-18 | 10090.29 | 9711.65 | 9673.68 | 9994.18 | 9959.87 | 49429.68 | -0.14 |
| 2013-12-19 | 10259.59 | 9948.10 | 9605.26 | 10209.36 | 10088.39 | 50110.70 | 1.38 |
| 2013-12-20 | 10056.43 | 9752.02 | 9684.21 | 10017.45 | 10024.13 | 49534.24 | -1.15 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 35384.54 | 60186.04 | 51144.70 | 38035.71 | 91657.85 | 276408.84 | -0.11 |
| 2023-12-08 | 35857.31 | 62113.01 | 51329.54 | 38140.49 | 89817.44 | 277257.78 | 0.31 |
| 2023-12-11 | 35530.01 | 61878.89 | 50917.20 | 38171.93 | 90387.70 | 276885.73 | -0.13 |
| 2023-12-12 | 35348.18 | 61374.64 | 51030.95 | 38240.03 | 90802.44 | 276796.24 | -0.03 |
| 2023-12-13 | 36002.77 | 61824.86 | 51898.29 | 38198.12 | 92098.51 | 280022.55 | 1.17 |
2483 rows × 7 columns
#Plot data:
plotly_line(portfolio_df, portfolio_df['Total Value [$]'], "Portfolio Total Value")
log_plotly_line(portfolio_df, portfolio_df['Total Value [$]'], "Portfolio Total Value - Log Scale")
plotly_line(portfolio_df, portfolio_df['Daily Return [%]'], "Daily Return [%]")
plotly_data(portfolio_df, "Equal Weighted Porfolio")
log_plotly_data(portfolio_df, "Equal Weighted Porfolio")
def rand_weights(n):
''' Produces n random weights that sum to 1 '''
k = np.random.rand(n)
return k / sum(k)
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
weights = rand_weights(n_assets).round(4)
display(weights)
sum(weights)
array([0.3312, 0.1094, 0.0768, 0.4263, 0.0563])
1.0
#Eqw -- Equal Weighted </p>
#Rdw_1 -- Random Weighted 1 </p>
#Rdw_2 -- Random Weighted 2 [...]
def random_port_generate(initial_investment, n_runs):
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
#lock vector to test function
eq_weights = np.ones(n_assets) * 1/n_assets
#Asset Allocation with parameters defined
Eqw_df = asset_allocation(Stock_Prices_df, initial_investment, eq_weights)[['Total Value [$]', 'Daily Return [%]']]
Eqw_df = Eqw_df.rename({'Total Value [$]':'Eqw [$]', 'Daily Return [%]':'Eqw [%]'}, axis="columns")
All_df = Eqw_df
# Placeholder to store all weights
weights_runs = np.zeros((n_runs, n_assets))
for i in range(n_runs):
# Generate random weights
weights = rand_weights(n_assets)
# Store the weights
weights_runs[i,:] = weights
# Random Asset Allocation
df = asset_allocation(Stock_Prices_df, initial_investment, weights)[['Total Value [$]', 'Daily Return [%]']]
#rename columns for iterate
Rdw = df.rename({'Total Value [$]':'Rdw_{}[$]'.format(i), 'Daily Return [%]':'Rdw_{}[%]'.format(i)}, axis="columns")
All_df = pd.merge(All_df, Rdw, on = 'Date')
#All_df = Eqw_df.join(Rdw)
print("Simulation Run = {}".format(i))
print("Weights = {}".format(weights_runs[i].round(3)))
print('\n')
return All_df
df = random_port_generate(initial_investment, n_runs)
daily_returns_df = df.iloc[:, 1::2]
total_values_df = df.iloc[:, 0::2]
display(df.round(2))
Simulation Run = 0 Weights = [0.337 0.125 0.018 0.185 0.335] Simulation Run = 1 Weights = [0.103 0.248 0.189 0.349 0.11 ] Simulation Run = 2 Weights = [0.055 0.352 0.404 0.189 0. ] Simulation Run = 3 Weights = [0.002 0.289 0.308 0.108 0.293] Simulation Run = 4 Weights = [0.249 0.202 0.185 0.221 0.142] Simulation Run = 5 Weights = [0.129 0.007 0.146 0.353 0.366] Simulation Run = 6 Weights = [0.397 0.244 0.285 0.063 0.012] Simulation Run = 7 Weights = [0.088 0.171 0.325 0.231 0.185] Simulation Run = 8 Weights = [0.194 0.433 0.029 0.123 0.222] Simulation Run = 9 Weights = [0.22 0.239 0.236 0.134 0.171]
| Eqw [$] | Eqw [%] | Rdw_0[$] | Rdw_0[%] | Rdw_1[$] | Rdw_1[%] | Rdw_2[$] | Rdw_2[%] | Rdw_3[$] | Rdw_3[%] | ... | Rdw_5[$] | Rdw_5[%] | Rdw_6[$] | Rdw_6[%] | Rdw_7[$] | Rdw_7[%] | Rdw_8[$] | Rdw_8[%] | Rdw_9[$] | Rdw_9[%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | |||||||||||||||||||||
| 2013-12-16 | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 | ... | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 | 50000.00 | 0.00 |
| 2013-12-17 | 49500.36 | -1.00 | 49683.11 | -0.63 | 49427.13 | -1.15 | 49206.32 | -1.59 | 49299.32 | -1.40 | ... | 49763.44 | -0.47 | 49456.78 | -1.09 | 49477.54 | -1.04 | 49236.13 | -1.53 | 49438.72 | -1.12 |
| 2013-12-18 | 49429.68 | -0.14 | 49870.52 | 0.38 | 49348.22 | -0.16 | 48853.15 | -0.72 | 49020.94 | -0.56 | ... | 49727.28 | -0.07 | 49358.49 | -0.20 | 49218.15 | -0.52 | 49367.54 | 0.27 | 49331.48 | -0.22 |
| 2013-12-19 | 50110.70 | 1.38 | 50712.38 | 1.69 | 50110.36 | 1.54 | 49381.60 | 1.08 | 49563.65 | 1.11 | ... | 50409.17 | 1.37 | 49959.91 | 1.22 | 49750.55 | 1.08 | 50307.20 | 1.90 | 49974.20 | 1.30 |
| 2013-12-20 | 49534.24 | -1.15 | 49968.87 | -1.47 | 49466.75 | -1.28 | 48958.29 | -0.86 | 49201.80 | -0.73 | ... | 49873.08 | -1.06 | 49366.62 | -1.19 | 49341.05 | -0.82 | 49509.14 | -1.59 | 49425.59 | -1.10 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 276408.84 | -0.11 | 290550.40 | 0.12 | 258257.44 | -0.12 | 254951.97 | -0.40 | 320899.79 | -0.31 | ... | 296892.10 | -0.03 | 233776.20 | -0.11 | 278772.13 | -0.26 | 296914.26 | -0.04 | 275122.27 | -0.15 |
| 2023-12-08 | 277257.78 | 0.31 | 289582.30 | -0.33 | 260231.07 | 0.76 | 258943.44 | 1.57 | 321328.56 | 0.13 | ... | 294212.70 | -0.90 | 237248.40 | 1.49 | 279353.51 | 0.21 | 299591.39 | 0.90 | 276660.84 | 0.56 |
| 2023-12-11 | 276885.73 | -0.13 | 289832.85 | 0.09 | 259750.93 | -0.18 | 257640.22 | -0.50 | 321205.75 | -0.04 | ... | 294792.58 | 0.20 | 235770.01 | -0.62 | 278900.30 | -0.16 | 299358.95 | -0.08 | 276044.51 | -0.22 |
| 2023-12-12 | 276796.24 | -0.03 | 289979.03 | 0.05 | 259487.71 | -0.10 | 256997.37 | -0.25 | 321295.92 | 0.03 | ... | 295620.35 | 0.28 | 235003.17 | -0.33 | 279034.96 | 0.05 | 298610.38 | -0.25 | 275775.75 | -0.10 |
| 2023-12-13 | 280022.55 | 1.17 | 293571.84 | 1.24 | 261847.60 | 0.91 | 259680.70 | 1.04 | 325165.05 | 1.20 | ... | 298987.33 | 1.14 | 238150.19 | 1.34 | 282267.38 | 1.16 | 301757.05 | 1.05 | 279137.01 | 1.22 |
2483 rows × 22 columns
plotly_data(total_values_df, "Portfolios Total Value[$]")
log_plotly_data(total_values_df, "Portfolios Total Value[$]")
plotly_data(daily_returns_df, "Portfolio Daily Returns [%]")
import plotly.express as px
# Plot histograms for stocks daily returns using plotly express
fig = px.histogram(daily_returns_df)
fig.update_layout({'plot_bgcolor': "white"})
#Random Asset Allocation 01
weights_1 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_1))
Rdw_1 = asset_allocation(Stock_Prices_df, initial_investment, weights_1)
Rdw_1.round(2)
Value Invested: $ 50000 Number of Stocks: 5 Weights: [0.1258 0.238 0.294 0.1939 0.1483]
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 6290.00 | 11900.00 | 14700.00 | 9695.00 | 7415.00 | 50000.00 | 0.00 |
| 2013-12-17 | 6290.00 | 11529.41 | 14545.26 | 9661.17 | 7379.36 | 49405.20 | -1.19 |
| 2013-12-18 | 6346.80 | 11556.86 | 14220.31 | 9689.36 | 7385.25 | 49198.58 | -0.42 |
| 2013-12-19 | 6453.28 | 11838.23 | 14119.73 | 9897.98 | 7480.54 | 49789.77 | 1.20 |
| 2013-12-20 | 6325.50 | 11604.90 | 14235.79 | 9711.92 | 7432.90 | 49310.99 | -0.96 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 22256.88 | 71621.39 | 75182.71 | 36875.62 | 67964.29 | 273900.89 | -0.23 |
| 2023-12-08 | 22554.25 | 73914.48 | 75454.42 | 36977.21 | 66599.63 | 275499.98 | 0.58 |
| 2023-12-11 | 22348.38 | 73635.88 | 74848.28 | 37007.68 | 67022.48 | 274862.70 | -0.23 |
| 2023-12-12 | 22234.01 | 73035.82 | 75015.49 | 37073.71 | 67330.01 | 274689.04 | -0.06 |
| 2023-12-13 | 22645.74 | 73571.59 | 76290.49 | 37033.08 | 68291.04 | 277831.94 | 1.14 |
2483 rows × 7 columns
#Random Asset Allocation 02
weights_2 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_2))
Rdw_2 = asset_allocation(Stock_Prices_df, initial_investment, weights_2)
Rdw_2.round(2)
Value Invested: $ 50000 Number of Stocks: 5 Weights: [0.1903 0.0861 0.2853 0.1239 0.3144]
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 9515.00 | 4305.00 | 14265.00 | 6195.00 | 15720.00 | 50000.00 | 0.00 |
| 2013-12-17 | 9515.00 | 4170.93 | 14114.84 | 6173.38 | 15644.45 | 49618.60 | -0.76 |
| 2013-12-18 | 9600.92 | 4180.87 | 13799.51 | 6191.40 | 15656.92 | 49429.60 | -0.38 |
| 2013-12-19 | 9762.00 | 4282.66 | 13701.90 | 6324.70 | 15858.95 | 49930.21 | 1.01 |
| 2013-12-20 | 9568.69 | 4198.24 | 13814.52 | 6205.81 | 15757.94 | 49545.21 | -0.77 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 33668.39 | 25910.09 | 72957.91 | 23563.12 | 144086.14 | 300185.66 | -0.17 |
| 2023-12-08 | 34118.23 | 26739.65 | 73221.59 | 23628.04 | 141193.01 | 298900.51 | -0.43 |
| 2023-12-11 | 33806.81 | 26638.86 | 72633.39 | 23647.51 | 142089.46 | 298816.02 | -0.03 |
| 2023-12-12 | 33633.79 | 26421.78 | 72795.65 | 23689.70 | 142741.43 | 299282.36 | 0.16 |
| 2023-12-13 | 34256.64 | 26615.60 | 74032.91 | 23663.74 | 144778.85 | 303347.74 | 1.36 |
2483 rows × 7 columns
#Random Asset Allocation 03
weights_3 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_3))
Rdw_3 = asset_allocation(Stock_Prices_df, initial_investment, weights_3)
Rdw_3.round(2)
Value Invested: $ 50000 Number of Stocks: 5 Weights: [0.1544 0.191 0.3766 0.2723 0.0057]
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 7720.00 | 9550.00 | 18830.00 | 13615.00 | 285.00 | 50000.00 | 0.00 |
| 2013-12-17 | 7720.00 | 9252.59 | 18631.79 | 13567.49 | 283.63 | 49455.50 | -1.09 |
| 2013-12-18 | 7789.71 | 9274.63 | 18215.54 | 13607.08 | 283.86 | 49170.81 | -0.58 |
| 2013-12-19 | 7920.40 | 9500.43 | 18086.71 | 13900.05 | 287.52 | 49695.11 | 1.07 |
| 2013-12-20 | 7763.57 | 9313.18 | 18235.37 | 13638.76 | 285.69 | 49236.55 | -0.92 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 27316.87 | 57477.67 | 96305.47 | 51785.62 | 2612.25 | 235497.87 | -0.28 |
| 2023-12-08 | 27681.84 | 59317.92 | 96653.52 | 51928.28 | 2559.80 | 238141.36 | 1.12 |
| 2023-12-11 | 27429.17 | 59094.34 | 95877.09 | 51971.08 | 2576.05 | 236947.72 | -0.50 |
| 2023-12-12 | 27288.79 | 58612.78 | 96091.27 | 52063.80 | 2587.87 | 236644.52 | -0.13 |
| 2023-12-13 | 27794.14 | 59042.74 | 97724.48 | 52006.75 | 2624.81 | 239192.92 | 1.08 |
2483 rows × 7 columns
#Random Asset Allocation 04
weights_4 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_4))
Rdw_4 = asset_allocation(Stock_Prices_df, initial_investment, weights_4)
Rdw_4.round(2)
Value Invested: $ 50000 Number of Stocks: 5 Weights: [0.282 0.0599 0.1778 0.2212 0.2591]
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 14100.00 | 2995.00 | 8890.00 | 11060.00 | 12955.00 | 50000.00 | 0.00 |
| 2013-12-17 | 14100.00 | 2901.73 | 8796.42 | 11021.41 | 12892.74 | 49712.29 | -0.58 |
| 2013-12-18 | 14227.32 | 2908.64 | 8599.90 | 11053.57 | 12903.01 | 49692.44 | -0.04 |
| 2013-12-19 | 14466.02 | 2979.45 | 8539.08 | 11291.55 | 13069.51 | 50345.62 | 1.31 |
| 2013-12-20 | 14179.57 | 2920.73 | 8609.26 | 11079.30 | 12986.27 | 49775.12 | -1.13 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 49892.21 | 18025.72 | 45467.64 | 42067.49 | 118742.74 | 274195.80 | -0.01 |
| 2023-12-08 | 50558.80 | 18602.85 | 45631.96 | 42183.39 | 116358.49 | 273335.48 | -0.31 |
| 2023-12-11 | 50097.32 | 18532.73 | 45265.39 | 42218.15 | 117097.26 | 273210.85 | -0.05 |
| 2023-12-12 | 49840.93 | 18381.70 | 45366.51 | 42293.48 | 117634.56 | 273517.19 | 0.11 |
| 2023-12-13 | 50763.91 | 18516.55 | 46137.58 | 42247.12 | 119313.61 | 276978.78 | 1.27 |
2483 rows × 7 columns
#Random Asset Allocation 02
weights_5 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_5))
Rdw_5 = asset_allocation(Stock_Prices_df, initial_investment, weights_5)
Rdw_5.round(2)
Value Invested: $ 50000 Number of Stocks: 5 Weights: [0.3105 0.2064 0.1784 0.0662 0.2384]
| ITSA4.SA | PETR4.SA | TAEE11.SA | VALE3.SA | WEGE3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2013-12-16 | 15525.00 | 10320.00 | 8920.00 | 3310.00 | 11920.00 | 49995.00 | 0.00 |
| 2013-12-17 | 15525.00 | 9998.61 | 8826.10 | 3298.45 | 11862.71 | 49510.88 | -0.97 |
| 2013-12-18 | 15665.18 | 10022.42 | 8628.92 | 3308.07 | 11872.17 | 49496.77 | -0.03 |
| 2013-12-19 | 15928.01 | 10266.44 | 8567.89 | 3379.30 | 12025.36 | 50167.00 | 1.35 |
| 2013-12-20 | 15612.61 | 10064.08 | 8638.31 | 3315.78 | 11948.77 | 49579.55 | -1.17 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 54934.50 | 62111.99 | 45621.07 | 12589.82 | 109256.15 | 284513.54 | -0.06 |
| 2023-12-08 | 55668.47 | 64100.62 | 45785.95 | 12624.50 | 107062.38 | 285241.93 | 0.26 |
| 2023-12-11 | 55160.34 | 63859.01 | 45418.14 | 12634.91 | 107742.14 | 284814.54 | -0.15 |
| 2023-12-12 | 54878.05 | 63338.63 | 45519.60 | 12657.45 | 108236.51 | 284630.24 | -0.06 |
| 2023-12-13 | 55894.31 | 63803.26 | 46293.27 | 12643.58 | 109781.42 | 288415.84 | 1.33 |
2483 rows × 7 columns
rand_value_df = pd.concat([ Eqw['Total Value [$]'],
Rdw_1['Total Value [$]'],
Rdw_2['Total Value [$]'],
Rdw_3['Total Value [$]'],
Rdw_4['Total Value [$]'],
Rdw_5['Total Value [$]']
], axis=1, join='inner').round(2)
#rand_value_df.to_csv('rand_value_df')
#read CSV file
rand_value_df = pd.read_csv('rand_value')
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
rand_value_df.set_index(['Date'], inplace = True)
log_plotly_data(rand_value_df, "Total Value [$]")
rand_daily_df = pd.concat([ Eqw['Daily Return [%]'],
Rdw_1['Daily Return [%]'],
Rdw_2['Daily Return [%]'],
Rdw_3['Daily Return [%]'],
Rdw_4['Daily Return [%]'],
Rdw_5['Daily Return [%]']
], axis=1, join='inner').round(2)
#rand_daily_df.to_csv('rand_daily')
#read CSV file
rand_daily_df = pd.read_csv('rand_daily')
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
rand_daily_df.set_index(['Date'], inplace = True)
plotly_data(rand_daily_df, "Total Value [$]")